<template>
  <div>
		<dicAddNew ref="dicAddNewRefs"
    :editOrAddForm="editOrAddForm"
    :labelView = "{level: '结束会话类别层级数：',
          content: '类别配置：',
          addItem: '新增类别项',
        }"
    :closeSession="true"
    :newModel="dicConfigData"></dicAddNew>
    <div style="text-align:center">
      <el-button type="primary" class="primary_btn" @click="saveFun">保存</el-button>
    </div>
  </div>
</template>
<script>
  import dicAddNew from '@/components/business-modules/config/dicConfig/dicAddNew.vue'
  export default {
    name: 'dicManage',
    data () {
      return {
        firstTitle: '添加字典',
        editOrAddForm: {
          _id: '',
          cascade: 1,
          name: '结束会话类型'
        },   // 修改添加字典数据
        dicConfigData: {
          options: [],
          _id: '',
          cascade: 1,
          name: '结束会话类型'
        }
      }
    },
    components: {
      dicAddNew,
    },
    methods: {
      saveFun () {
        let data = this.$refs.dicAddNewRefs.onSave()
        if (data) {
          data.type = 'webchat'
          this.$store.dispatch('newSaveDicItem', data).then((res) => {
            if (res.success) {
              this.$message.success('保存成功')
            }
          })
        }
			},
			intData () {
        this.$store.dispatch('getDicByType', {type: 'webchat'}).then((res) => {
          if (res.success && res.row) {
            res.row.cascade = res.row.cascade || '3'
            res.row.name =  res.row.name || '结束会话类型'
            optionState(res.row.options)
            this.editOrAddForm._id = res.row._id
            this.editOrAddForm.name = res.row.name
            this.editOrAddForm.cascade = res.row.cascade || '3'
            this.dicConfigData = res.row
          }
        })
			},
      updata (val) {
        let id = val._id
        this.isNewDic = false
        this.$store.dispatch('dicfindOneAppDic', {_id: id}).then(res => {
          if (res.success) {
            let editData = this.tableData.filter(item => item._id === id)[0]
            this.editOrAddForm._id = editData._id
            this.editOrAddForm.cascade = editData.cascade.toString()
            this.editOrAddForm.name = editData.name
            this.firstTitle = '编辑字典'
            this.dicConfigData = res.data
          }
        })
      }
    },
    beforeMount () {
      this.intData()
    }
  }
  function optionState(arr) {
    arr.forEach((item) => {
      if (!item.state) {
        item.state = '2'
      }
      if (item.options && item.options.length) {
        optionState(item.options)
      }
    })
  }
</script>
<style lang="stylus" scoped>
	@import "../../../../assets/common.styl" 
</style>